"""自定义迭代器"""


class SkillIterator:
    """迭代器"""

    def __init__(self, skill_list):
        self.__skill_list = skill_list
        self.__count = 0

    def __next__(self):
        """迭代器，返回容器下一个数据"""
        # 4.如果容器循环完成，则抛一个异常表示完成, -1 是防止越界
        if self.__count > len(self.__skill_list) - 1:
            raise StopIteration

        # 2.返回当前容器中第一个元素
        so = self.__skill_list[self.__count]
        # 3.容器下标加1，以便循环下一个元素
        self.__count += 1
        return so


class Skill:
    """技能对象"""
    pass


class skillManager:
    """技能管理器"""

    def __init__(self):
        """技能列表"""
        self.__skills = []

    def add_skill(self, skill):
        """添加技能"""
        self.__skills.append(skill)

    def __iter__(self):
        """可迭代对象"""
        # 1.创建迭代器,并传递需要迭代的数据
        # return SkillIterator(self.__skills)

        # 也可以利用yield进行迭代
        ls = 0
        while ls < len(self.__skills):
            yield self.__skills[ls]
            ls += 1

        """
            也可以用for语句来进行迭代
            for i in self.__skills:
                yield i
            
        """


manager = skillManager()
manager.add_skill(Skill())
manager.add_skill(Skill())
manager.add_skill(Skill())

man = manager.__iter__()
while True:
    try:
        lp = man.__next__()
        print(lp)
    # 5.这里接收并处理异常
    except StopIteration:
        break
